package com.alibaba.shopping_goods_service.service;

import com.alibaba.shopping_common.pojo.Brand;
import com.alibaba.shopping_common.result.BusException;
import com.alibaba.shopping_common.result.CodeEnum;
import com.alibaba.shopping_common.service.BrandService;
import com.alibaba.shopping_goods_service.mapper.BrandsMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @ClassName GoodsServiceImpl
 * @Description TODO
 * @Author Leo
 * @Date 2024/12/5 12:47
 */
@Transactional // 事务管理
@DubboService // 声明是一个dubbo服务，并且在启动时注册在nacos上
public class BrandsServiceImpl implements BrandService {

    @Autowired
    private BrandsMapper brandsMapper;

    @Override
    public Brand findByID(Long id) {
        return brandsMapper.selectById(id);
    }

    // 新增品牌信息
    @Override
    public void addBrand(Brand brand) {
        brandsMapper.insert(brand);
    }

    // 删除品牌信息
    @Override
    public void delBrand(Long id) {
        brandsMapper.deleteById(id);
    }

    // 更新品牌信息
    @Override
    public void updateBrand(Brand brand) {
        brandsMapper.updateById(brand);
    }

    // 查询所有的品牌信息
    @Override
    public List<Brand> findAll() {
        return brandsMapper.selectList(null);
    }

    /**
     * 分页查询
     * @param brand 查询条件
     * @param page 页码
     * @param size 页面大小
     * @return
     */
    @Override
    public Page<Brand> search(Brand brand, int page, int size) {
        QueryWrapper<Brand> wrapper = new QueryWrapper<>();
        // 当brand不为空时，传入查询条件
        if (brand != null && StringUtils.hasText(brand.getName())){
            wrapper.like("name",brand.getName());
        }

        // 根据传入的条件进行分页查询
        return brandsMapper.selectPage(new Page<>(page,size),wrapper);
    }
}




